var newdate={};
var employees={};
$(function(){

	beginning();
    /**点击上一月*/
    $('.hudate-deahear').on('click','.previous',function(event){
       var hert=$(".date-moner").html().split("-");
       if(parseInt(hert[1])-1==0){
          var paertm=12;
          var paertt=parseInt(hert[0])-1;
       }else{
          var paertm=parseInt(hert[1])-1;
          var paertt=hert[0];
       }
       newDate(paertt+"-"+paertm);
       event.stopPropagation();   
    });
   /**点击下一月*/
   $('.hudate-deahear').on('click','.next',function(event){
      var hert=$(".date-moner").html().split("-");
      if(parseInt(hert[1])+1>12){
          var paertm=1;
          var paertt=parseInt(hert[0])+1;
      }else{
          var paertm=parseInt(hert[1])+1;
          var paertt=hert[0];
      }
      newDate(paertt+"-"+paertm);
      event.stopPropagation();   
   });

   $('.hudate-date').on('click','.click-ctear tr td',function(event){
      if(!$(this).is(".close")){
          $(".click-ctear tr td").removeClass("active");
          $(this).addClass("active");
          $(".date-submit").attr("value",$(".date-moner").html()+"-"+$(this).children(".date-time").html());
          var date_time = $(".date-moner").html().split("-");
          $(".crenprice").html(Building_type(date_time[0],date_time[1],$(this).children(".date-time").html()));
          $(".crenpriert").html(Building_typearci($(".date-moner").html()+"-"+$(this).children(".date-time").html())); 
          $(".price_id").attr("value",Building_priceId($(".date-moner").html()+"-"+$(this).children(".date-time").html()));
          $('.disabled-test').attr('value','1');
          $('.disabled-twe').attr('value','0');
          price();
          ajaxGetLineStock();
          $(".footer-display-none").hide();
          $(".footer-display").show();
      }
      event.stopPropagation();
    });
});

/*  以下是计算过程  */
function Judge(){
    var newdaer;
    if(!newdate.date){
        newdaer=Circle.thisyear().toString()+'-'+Circle.thismonth().toString()+'-'+Circle.thisday().toString();
    }else{
        newdaer=newdate.date;
    }
    return newdaer;
}

function Juday(){
    var newday;
    if(!newdate.close){
        newday=30;
    }else{
        newday=newdate.close;
    }
    return newday;
}
 
function beginning(){     
    var newdaer=Judge();    /* 判断时间 */
    var newday=Juday();      /* 判断显示天数 */
    newDate(newdaer,newday);    //初始化日历

}

function newDate(newdaer,newday){
    /* 清除一切样式 */
    $(".click-ctear tr td").removeClass("close");
    $(".click-ctear tr td").removeClass("active");
    $(".date-time").html("");
    $(".Price-time").html("");
    var exergue =newdaer.split("-");
    $(".date-moner").html(exergue[0]+"-"+exergue[1]);   /* 初始化月 */  
    /* 循环日历 */
    var a=1;
    for(var j=0;j<6;j++){
      for(var i=0;i<7;i++){
          if(j==0&&i<getfirstday(exergue[0],exergue[1])){
              $(".hudate-date tbody tr").find("td").eq(i).addClass("close");
          }else{
            if(a<=getdaysinonemonth(exergue[0],exergue[1])){
                  $(".hudate-date tbody tr").eq(j+1).find("td").eq(i).find(".date-time").html(a);
              if(Building_type(exergue[0],exergue[1],a)!=false && Building_type(exergue[0],exergue[1],a)!=null){
                  var active=Buildingdate(exergue[0]+"-"+exergue[1]+"-"+a.toString());
                  if(active==$(".date-io").html()){
                      $(".hudate-date tbody tr").eq(j+1).find("td").eq(i).addClass("active");
                  }
                  $(".hudate-date tbody tr").eq(j+1).find("td").eq(i).find(".Price-time").html("¥"+Building_type(exergue[0],exergue[1],a));
              }else{
                  $(".hudate-date tbody tr").eq(j+1).find("td").eq(i).addClass("close");
              }
              a++;
            }else{
              $(".hudate-date tbody tr").eq(j+1).find("td").eq(i).addClass("close");
              a++;
            }
          }
      }
    } 
}

/*  以下是各个工具类 */

//算某个月的总天数
function getdaysinonemonth(year,month){ 
	  month=parseInt(month,10);
	  var d=new Date(year,month,0);
	  return d.getDate();
}

//计算某个月的第一天是星期几
function getfirstday(year,month){
	  month=month-1;
	  var d=new Date(year,month,1);
	  return d.getDay();
}

// 初始化时当前的年月日
var Circle={  
	  "newdate":new Date(),  
	  "thisyear":function(){  
	          return this.newdate.getFullYear();
	        },
	  "thismonth":function(){
	          return this.newdate.getMonth()+1;  
	        },
	  "thisday":function(){
	          return this.newdate.getDate();
	        }
};  

function Buildingdate(Ndate){
  var Ndate_nz = Ndate.split("-");
  if(Ndate_nz[1]<10){
    Ndate_nz[1]="0"+Ndate_nz[1];
  }
  if(Ndate_nz[2]<10){
    Ndate_nz[2]="0"+Ndate_nz[2];
  }
  return Ndate_nz[0]+"-"+Ndate_nz[1]+"-"+Ndate_nz[2];
}

//传入数据
function Building_type(thisyear,thismonth,thisdate){  
      var Room_data=thisyear+"-"+thismonth+"-"+thisdate;
      for(var UT=0;UT<employees.length;UT++){
    	var employees_i= employees[UT].DateWorth.split("-");
    	var employees_num=employees_i[0]+"-"+parseInt(employees_i[1])+"-"+parseInt(employees_i[2]);
    	  
        if(employees_num==Room_data&&employees[UT].Room!=0){
           return employees[UT].Price;
        }else{
           var falseor=false
        }
      }
      return falseor;
}

function Building_typear(Room_date){  
      for(var UT=0;UT<employees.length;UT++){
      	var employees_i= employees[UT].DateWorth.split("-");
    	var employees_num=employees_i[0]+"-"+parseInt(employees_i[1])+"-"+parseInt(employees_i[2]);
        if(employees_num==Room_date&&employees[UT].Room!=0){
           return employees[UT].Room;
        }else{
           var falseor=false
        }
      }
      return falseor;
}

function Building_typearci(Room_date){  
      for(var UT=0;UT<employees.length;UT++){
        var employees_i= employees[UT].DateWorth.split("-");
        var employees_num=employees_i[0]+"-"+parseInt(employees_i[1])+"-"+parseInt(employees_i[2]);
        if(employees_num==Room_date&&employees[UT].Room!=0){
           return employees[UT].Child;
        }else{
           var falseor=false
        }
      }
      return falseor;
}

function Building_priceId(Room_date){  
    for(var UT=0;UT<employees.length;UT++){
      var employees_i= employees[UT].DateWorth.split("-");
      var employees_num=employees_i[0]+"-"+parseInt(employees_i[1])+"-"+parseInt(employees_i[2]);
      if(employees_num==Room_date&&employees[UT].Room!=0){
         return employees[UT].price_id;
      }else{
         var falseor=false
      }
    }
    return falseor;
}

function ajaxGetLineStock(){
	
	var price_id = $('input[name=price_id]').val();
	var url = hostUrl()+'line/lineproduct/ajaxGetLineStock';
	$.ajax({
		type:'get',
		url :url,
		data:{'price_id':price_id},
		dataType:'json',
		async: false,
		success:function(json){
		    if(json.status){
		        $('.price-xq').html(json.content);
		        $('.price-xq').show();
		    }
		}
	});
}


